package cn.ffcs.uec.outStaff.action;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Messagebox;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.uec.outStaff.action.bean.UecOutStaffCostHEditBean;
import cn.ffcs.uec.outStaff.manager.UecOutStaffManager;
import cn.ffcs.uec.outStaff.model.UecOutStaffCostB;
import cn.ffcs.uec.outStaff.model.UecOutStaffCostH;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.PubUtil;
import cn.ffcs.uom.common.vo.NodeVo;
import cn.ffcs.uom.systemconfig.manager.IdentityCardConfigManager;

@Controller
@Scope("prototype")
public class UecOutStaffCostHEditComposer extends BasePortletComposer {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3695440774892813761L;

	private UecOutStaffCostHEditBean bean = new UecOutStaffCostHEditBean();

	private String opType;

	@Resource
	private UecOutStaffManager uecOutStaffManager;

	@Resource
	private IdentityCardConfigManager identityCardConfigManager;


	private UecOutStaffCostB uecOutStaffCostB;
	
	private UecOutStaffCostH uecOutStaffCostH;

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		Components.wireVariables(comp, bean);
		
		
	}

	/**
	 * window初始化.
	 * 
	 * @throws Exception
	 *             异常
	 */
	public void onCreate$uecOutStaffCostHEditWin() throws Exception {
		this.bindCombobox();
		opType = (String) arg.get("opType");
		if (opType.equals("add")) {
			bean.getUecOutStaffCostHEditWin().setTitle("新增");
			
		} else if (opType.equals("edit")) {
			
			bean.getUecOutStaffCostHEditWin().setTitle("修改");
			PubUtil.fillBeanFromPo(arg.get("uecOutStaffCostH"), bean);
		} else if (opType.equals("view")) {
			bean.getUecOutStaffCostHEditWin().setTitle("查看");
			PubUtil.disableComponent(bean.getUecOutStaffCostHEditWin());
			PubUtil.fillBeanFromPo(arg.get("uecOutStaffCostH"), bean);
		}
	}
	

	/**
	 * 保存.
	 * 
	 * @throws Exception
	 */
	public void onOk() throws Exception {
		String msg = checkUacStaffCostHData();
		if (msg != null) {
			Messagebox.show(msg);
			return;
		}
		uecOutStaffCostB = (UecOutStaffCostB) arg.get("uecOutStaffCostB");
		uecOutStaffCostH = UecOutStaffCostH.newInstance();
		uecOutStaffCostH = (UecOutStaffCostH) arg.get("uecOutStaffCostH");
		UecOutStaffCostH uecOutStaffCostHObj = UecOutStaffCostH.newInstance();
		if (uecOutStaffCostH != null) {
			PubUtil.fillPoFromEditorBean(uecOutStaffCostH, bean);
		} else {
			uecOutStaffCostH = uecOutStaffCostHObj;
			//uecOutStaffCostH.setOutStaffWorkId(uecOutStaffWork.getId());
			PubUtil.fillPoFromEditorBean(uecOutStaffCostH, bean);
		}
		if (opType.equals("add")) {
			//uecOutStaffCostH.setOutStaffWorkId(uecOutStaffWork.getId());
			uecOutStaffCostH.setId(null);
			
			uecOutStaffCostH.setStatusCd("1000");
			uecOutStaffCostH.setMonthId(null);
			uecOutStaffCostH.addOnly();
			uecOutStaffCostB.setPkOutStaffCostHId(uecOutStaffCostH.getId());
			uecOutStaffCostB.updateOnly();
			Events.postEvent("onOK", this.self, uecOutStaffCostH);
			onCancel();
			Messagebox.show("新增成功！");
		} else if (opType.equals("edit")) {
			uecOutStaffCostH.setStatusCd("1000");
			uecOutStaffCostH.setMonthId(null);
			uecOutStaffCostH.updateOnly();
			Events.postEvent("onOK", this.self, uecOutStaffCostH);
			onCancel();
			Messagebox.show("修改成功！");
		}
		
		onCancel();
	}

	/**
	 * 取消.
	 */
	public void onCancel() {
		bean.getUecOutStaffCostHEditWin().onClose();
	}

	private String checkUacStaffCostHData() {
		// BaseInfo Check
		/*if (StrUtil.isNullOrEmpty(bean.getJobName().getValue())) {
			return "请填写岗位名称";
		} else if (StrUtil.checkBlank(bean.getJobName().getValue())) {
			return "岗位名称中有空格";
		} else if (StrUtil.isNullOrEmpty(bean.getBeginDate().getValue())) {
			return "请填写在本单位从事外包开始日期";
		} else if (StrUtil.isNullOrEmpty(bean.getIsSCEO().getValue())) {
			return "请填写是否最小CEO";
		} else if (StrUtil.checkBlank(bean.getJobName().getValue())) {
			return "是否最小CEO中有空格";
		}*/

		// IDCARD Real Name Check
		/*if (EnumUacStaffInfo.CERT_TYPE_IDCARD.getValue().equals(
				bean.getCertType().getSelectedItem().getValue())) {
			String idNum = bean.getCertNumber().getValue();
			String idName = bean.getCertName().getValue();
			try {
				boolean isRealName = CertUtil.checkIdCard(idNum, idName);

				if (!isRealName) {
					return "实名认证未通过";
				}
			} catch (Exception e) {
				return "调用国政通接口失败";
			}

		}*/

		return null;
	}
	/**
	 * 绑定下拉框 绑定combobox
	 */
	private void bindCombobox() {
		List<NodeVo> nodeVoIsSendList = new ArrayList<NodeVo>();
		String[][] isSend = {{"1", "是"}, {"0", "否"}};
		for (int i = 0; i < isSend.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(isSend[i][j]);
				nodeVo.setName(isSend[i][j + 1]);
			}
			nodeVoIsSendList.add(nodeVo);
		}
		
		ListboxUtils.rendererForEdit(bean.getGrantflag(),
				nodeVoIsSendList);
		ListboxUtils.rendererForEdit(bean.getSendflag(),
				nodeVoIsSendList);
	}
}
